//
//  WQCUserViewModel.swift
//  3-微博
//
//  Created by DDDD on 16/2/27.
//  Copyright © 2016年 gongsimingzi. All rights reserved.
//

import UIKit
import AFNetworking

class WQCUserViewModel: NSObject {

    static let userViewModel = WQCUserViewModel()
    
    var userModel : WQCUserModel?
    ///
    var access_token : String? {
        
        return userModel?.access_token
        
        }
    /// 用户是否登录 true(access_token 为空)标示登录
    var isLogin : Bool {
        return  access_token == nil ? true : false
    }

    
    override  init (){
        userModel = WQCUserModel.readObject("info.aaaa")
    }
        
    func loadToken (code:String,success:()->(),failed:()->()){
        let manager = AFHTTPSessionManager()
        /*client_id	true	string	申请应用时分配的AppKey。
        client_secret	true	string	申请应用时分配的AppSecret。
        grant_type	true	string	请求的类型，填写authorization_code
        
        grant_type为authorization_code时
        必选	类型及范围	说明
        code	true	string	调用authorize获得的code值。
        redirect_uri	true	string	回调地址，需需与注册应用里的回调地址一致。*/
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        let parame = ["client_id":app_KEY,
            "client_secret":app_SECERT,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":app_URL
        ]
        manager.POST("https://api.weibo.com/oauth2/access_token", parameters: parame, progress: { (_) -> Void in
            
            }, success: { (_, JSON) -> Void in
                //                print(JSON)
                if let Odic = JSON as? [String : NSObject]{
                    
                    let model = WQCUserModel(dic: Odic)
                    
                    self.loadUserInfo(model,success: success,failed: failed)
                }
                
            }) { (_, error) -> Void in
                print(error)
        }
    }

    
    func loadUserInfo(userModel  : WQCUserModel,success :()->() ,failed :()->()){
        
        let afn = AFHTTPSessionManager()
        
        /*access_token	true	string	采用OAuth授权方式为必填参数，OAuth授权后获得。
        uid	false	int64	需要查询的用户ID。
        参数uid与screen_name二者必选其一，且只能选其一；
        接口升级后，对未授权本应用的uid，将无法获取其个人简介、认证原因、粉丝数、关注数、微博数及最近一条微博内容。*/
        
        let parame = ["access_token":userModel.access_token ?? "",
            "uid":userModel.uid ?? ""
        ]
        afn.GET("https://api.weibo.com/2/users/show.json", parameters: parame, progress: nil, success: { (_, JSON) -> Void in
            //            print(JSON )
            
            //归档保存
            if let Odic = JSON as? [String : NSObject]{
                userModel.profile_image_url = Odic["profile_image_url"] as? String
                userModel.screen_name = Odic["screen_name"] as? String
            }
            success()
            userModel.save("info.aaaa")
            }) { (_, error) -> Void in
                print(error )
                failed()
        }
        
    }

}
